﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XingFuStudio.Pioneer.Applications.City.Shop;

namespace XingFuStudio.Pioneer.Applications.City.Element {
    /// <summary>
    /// 商业信息实体
    /// </summary>
    public class GrayInfo : CityElement {
        /// <summary>
        /// 上货的时间
        /// </summary>
        public DateTime Time {
            get;
            set;
        }

        /// <summary>
        /// 是否可以收获
        /// </summary>
        public bool GetCanHarvest(List<ShopItem> grays) {
            if (this.Time != DateTime.MinValue) {
                var res = grays.Where(s => s.ID == this.ElementId);

                if (res.Count() > 0) {
                    GrayShopInfo info = (GrayShopInfo)res.First();
                    return this.Time.AddMinutes(info.TimeSpan) < DateTime.Now;
                } else {
                    return this.Time.AddMinutes(720) < DateTime.Now;
                }
            } else {
                return false;
            }
        }

        /// <summary>
        /// 是否可以投递货物
        /// </summary>
        public bool CanDeliver {
            get;
            set;
        }
        
        /// <summary>
        /// 初始化元素自身数据
        /// </summary>
        /// <param name="elementObject"></param>
        public override void InitElement(Dictionary<string, object> elementObject) {
            //调用基类方法
            base.InitElement(elementObject);

            //赋值自身属性
            if ((string)elementObject["collecttime"] == "0000-00-00 00:00:00") {
                this.Time = DateTime.MinValue;
            } else {
                this.Time = Convert.ToDateTime((string)elementObject["collecttime"]);
            }

            if (this.Time == DateTime.MinValue) {
                this.CanDeliver = true;
            }
        }
    }
}
